import {
    goodsExchangeGoodsByRecords
} from "../../services/mallDetailReq";

const {
    getBuyGoods,
    goodsInfoById
} = require('../../services/mallDetailReq')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsObj: {},
        buyInfo: {
            goodOrderAddress: "",
            goodOrderNumber: 1,
            goodOrderPrice: null,
            goodsId: null,
        },
        isGifts: 0

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 获取活动详情
        wx.showLoading({
            title: '加载详情中'
        })

        goodsInfoById(options.id, res => {
            console.log(res.data.data.goodsType)
            wx.hideLoading()
            this.setData({
                goodsObj: res.data.data,
            })
            let buyInfo = this.data.buyInfo
            let isGifts = this.data.isGifts
            buyInfo['goodsId'] = res.data.data.goodsId
            buyInfo['goodOrderPrice'] = res.data.data.goodsPrice
            isGifts = res.data.data.goodsType

            this.setData({
                buyInfo,
                isGifts
            })

        })

    },
    handlePay() {
        let params = this.data.buyInfo
        if (!params.goodOrderAddress) return wx.showToast({
            title: '请输入收货地址',
            icon: "error"
        })
        if (!params.goodOrderNumber) return wx.showToast({
            title: '请输入购买数量',
            icon: "error"
        })

        // console.log(params)
        let req = {
            address: params.goodOrderAddress,
            goodsId: params.goodsId,
            number: params.goodOrderNumber
        }
        wx.showLoading({
            title: '下单中',
        })
        // console.log(req)
        getBuyGoods(req, res => {
            if (res.data.data.code !== 0) return wx.showToast({
                title: res.data.msg,
                icon: "error"
            })
            // console.log(res)
            let req = res.data.data;
            // console.log(req)
            req.data["package"] = "prepay_id=" + req.data["package"]

            // console.log(req)
            wx.requestPayment({
                ...req.data,
                paySign: req.data.sign,
                success(res) {
                    // console.log(res)
                    return wx.showToast({
                        title: '支付成功',
                    })
                },
                fail(res) {
                    return wx.showToast({
                        title: '你已经取消支付',
                        icon: "error"
                    })
                },
            })
            // console.log(res)
        })
    },
    goodOrderNumberChange(e) {
        let buyInfo = this.data.buyInfo
        buyInfo['goodOrderNumber'] = e.detail
        this.setData({
            buyInfo
        })
    },
    goodOrderAddressChange(e) {
        let buyInfo = this.data.buyInfo
        // console.log(e)
        // console.log(e)
        buyInfo.goodOrderAddress = e.detail
        this.setData({
            buyInfo
        })
    },
    handleScorePay() {
        let params = this.data.buyInfo
        if (!params.goodOrderAddress) return wx.showToast({
            title: '请输入收货地址',
            icon: "error"
        })
        if (!params.goodOrderNumber) return wx.showToast({
            title: '请输入购买数量',
            icon: "error"
        })
        params["goodId"] = params["goodsId"];
        params["postAddress"] = params["goodOrderAddress"];
        params["redeemNum"] = params["goodOrderNumber"];
        wx.showLoading({
            title: '正在兑换中……',
            mask: true
        })
        goodsExchangeGoodsByRecords(params, (res) => {
            // console.log(`res-->`, res)
            if (res.data.code !== 0) return wx.showToast({
                title: res.data.msg,
                icon: "error"
            })
            return wx.showToast({
                title: '购买成功',
                icon: "success"
            })
        })


    }
})